package com.joezhou.account.dao.impl;

import com.joezhou.account.dao.AccountDao;
import com.joezhou.jdbc.v4.JdbcTemplate;

import java.util.List;
import java.util.Map;

/**
 * @author JoeZhou
 */
public class AccountDaoImpl implements AccountDao {

    private JdbcTemplate jdbcTemplate = new JdbcTemplate();

    @Override
    public List<Map<String, Object>> selectAll() {
        String sql = "select id, username, password, create_date from account";
        return jdbcTemplate.selectForList(sql);
    }

    @Override
    public int insert(String username, String password) {
        String sql = "insert into account (" +
                "username, password, create_date) values (?, ?, now())";
        return jdbcTemplate.update(sql, username, password);
    }

    @Override
    public int deleteById(int id) {
        String sql = "delete from account where id= ?";
        return jdbcTemplate.update(sql, id);
    }

    @Override
    public int updateById(String username, String password, int id) {
        String sql = "update account set username= ?, password= ? " +
                "where id= ?";
        return jdbcTemplate.update(sql, username, password, id);
    }
}